package com.yxwl.demo.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yxwl.demo.R;
import com.yxwl.demo.utils.DensityUtil;
import com.yxwl.demo.utils.StringUtil;


public class CustomProgress extends Dialog {
    private static CustomProgress dialog;
    private static String msg = "";
    private static TextView tv;

    private static int styleId = R.style.dialog;
    private static int width = DensityUtil.dp2px(100);
    private static int height = DensityUtil.dp2px(100);
    public static boolean cancelable = true;
    private static int layoutId = R.layout.layout_progress_dialog;
    private static View view;



    public CustomProgress(@NonNull Context context) {
        super(context, styleId);
    }

    public CustomProgress(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CustomProgress(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }


    protected void onCreate(Bundle var1) {
        super.onCreate(var1);
        this.setCancelable(cancelable);//弹出后会点击屏幕或物理返回键，dialog不消失
        this.setCanceledOnTouchOutside(false);//弹出后会点击屏幕，dialog不消失；点击物理返回键dialog消失

        if (StringUtil.isSpace(msg)) {
            initView();
        } else {
            initViewTips();
        }
    }

    private void initViewTips() {
        View view = this.getLayoutInflater().inflate(R.layout.layout_progress_tips_dialog1, null);
        tv = view.findViewById(R.id.m);
        if (msg == null || msg.isEmpty()) {
            tv.setVisibility(View.GONE);
        }else {
            tv.setText(msg);
            tv.setVisibility(View.VISIBLE);
        }
        this.setContentView(view);
        Window window = this.getWindow();
        if (window != null) {
            window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            window.setGravity(Gravity.CENTER);
        }
    }

    private void initView() {
        if (view == null) {
            view = this.getLayoutInflater().inflate(layoutId, null);
        }
        this.setContentView(view);

        Window window = getWindow();
        if (window != null) {
            if (width != 0 && height != 0) {
                window.setLayout(width, height);
            }
            window.setGravity(Gravity.CENTER);//设置显示位置
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (cancelable && keyCode == KeyEvent.KEYCODE_BACK) {
            dismissPD();
        }
        return super.onKeyDown(keyCode, event);
    }



    public static boolean isShow() {
        return dialog != null && dialog.isShowing();
    }



    public static void showPD(Activity act, String s) {
        if (act == null) return;
        dismissPD();
        msg = s;
        dialog = new CustomProgress(act);
        dialog.show();
    }


    public static void updatePD(String s) {
        if (isShow() && tv != null) {
            tv.setText(s);
        }
    }

    public static void dismissPD() {
        try {
            if (isShow()) {
                dialog.dismiss();
            }
        } catch (Exception e) {
        } finally {
            tv = null;
            msg = "";
            dialog = null;
            view = null;
        }
    }

}

